<template>
  <div>
    <div class="mb-2">
      <el-input 
      class="w-75 "
        v-model="comment.comContent"
        :autosize="{ minRows: 2, maxRows: 5 }"
        :type="textarea"
        placeholder="请输入评论"
      />
      <el-button @click="addComment" class="my-4 mx-3">发布</el-button>
    </div>

    <el-card
      v-for="(comment, index) in comments"
      :key="comment.noteCommentId"
      class="mb-2"
    >
      <el-row class="mb-1">
        <el-col :span="24">
          <img :src="comment.headUrl" style="height: 3rem; width: 3rem" />
          {{ comment.authorName }}
        </el-col>
      </el-row>
      <el-row class="mb-1">
        <el-col :span="24">{{ comment.comContent }}</el-col>
      </el-row>
      <el-row class="mb-1">
        <el-col :span="24">{{ index + 1 }}楼{{ comment.comTime }}</el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "NoteComment",
  props: {
    noteInfoId: Number,
  },
  data() {
    return {
      comments: [],
      comment: {
        comContent: null,
        newsId: this.newsId,
        relyAuthorId: 0,
      },
    };
  },
  mounted() {
    this.getComments();
  },
  methods: {
    getComments() {
      this.$axios
        .get("/api/home/comment?newsId=" + this.newsId)
        .then((response) => {
          let data = response.data;
          if (data.code == 200) {
            this.comments = data.data;
          }
        });
    },
    addComment() {
      this.$axios
        .post("/api/news/detail/comment/add",this.comment)
        .then((response) => {
          let data = response.data;
          alert(data.msg);
          if (data.code == 200) {
            this.getComments();
          } else if (data.code == 403) {
            this.$router.push("/");
          }
        });
    },
  },
};
</script>